package com.lib.online.dao.jdbc;

import java.util.*;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.lib.online.dao.LibDAO;
import com.lib.online.model.*;

/**
 * @author ievgen_s
 */
//@Repository
public class LibDAODBimpl { 
  /*    
    private static final String SQL_INSERT_USER =
	    "insert into users (first_name, last_name, password) values (?, ?, ?)";
    private static final String SQL_INSERT_BOOK =
            "insert into readings (title, pages, pub_date, isbn)  values"
            + " (?, ?, ?, ?)";
    private static final String SQL_SELECT_USER =  "SELECT user_id, first_name, last_name, password "
    	    + "FROM users WHERE first_name like :lastName";
    private static final String SQL_SELECT_BOOK =  "SELECT book_id, title, pages, pub_date, isbn "
    	    + "FROM readings WHERE title like :title";
    private static final String SQL_SELECT_BOOKS =  "SELECT book_id, title, pages, pub_date, isbn FROM readings";
    private static final String SQL_SELECT_AUTHORS =  "SELECT author_id, first_name, last_name, birth, death FROM authors";
    
   
    private DataSource dataSource;
    @Autowired  
    private JdbcTemplate jdbcTemplate;
   
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
         
    @Autowired    
    public LibDAODBimpl(DataSource dataSource) {
	
	 
	this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	
	 
    }

    public void addUser(User user) {
	jdbcTemplate.update(SQL_INSERT_USER, 
		user.getUserFName(),
		user.getUserLName(),
		user.getPassword()
		);
    }

    public User getUser(String uName) {
	  Map<String, Object> params = new HashMap<String, Object>();
	        params.put("lastName", uName + "%");
	        User user = this.namedParameterJdbcTemplate.queryForObject(SQL_SELECT_USER, params,
	                ParameterizedBeanPropertyRowMapper.newInstance(User.class)
	        );
	        
	        return user;
	
    }

    public void addUBook(String title, User user) {
	//soon...
     }

    public void removeUBook(String title, User user) {
	//soon...

    }

    public List<Reading> getUBooks(User user) {
	
	return null;
    }

    public void addBook(Reading book) {
	jdbcTemplate.update(SQL_INSERT_BOOK, 
		book.getTitle(),
		book.getPages(),
		book.getPubDate(),
		book.getIsbn()
		);
    }

    public Reading getBook(String title) {
	  Map<String, Object> params = new HashMap<String, Object>();
	        params.put("title", title + "%");
	        Reading book = this.namedParameterJdbcTemplate.queryForObject(SQL_SELECT_BOOK, params,
	                ParameterizedBeanPropertyRowMapper.newInstance(Reading.class)
	        );
	        
	        return book;
	
	
    }

    public void removeBook(String title) {
	//soon...
    }

    public List<Reading> getAllBooks() {
			
	List<Reading> resBooks =  this.namedParameterJdbcTemplate.query(SQL_SELECT_BOOKS, new HashMap<String, Object>(),
                ParameterizedBeanPropertyRowMapper.newInstance(Reading.class)
        );
	
	return resBooks;
    }

    public List<Author> getAuthors(String title) {
	 //stub...
	
	List<Author> resAuthors =  this.namedParameterJdbcTemplate.query(SQL_SELECT_AUTHORS, new HashMap<String, Object>(),
                ParameterizedBeanPropertyRowMapper.newInstance(Author.class)
        );
	
	return resAuthors;
	
    }

    public List<User> getReaders(String title) {
	 //not so soon...
	return null;

    }           
    
    
  */
   
}